package Day13_StringBuilder类;

//定义一个方法,把数组的内容遍历,拼接成一个字符串
//int[] arr={1,2,3} --> 打印成字符串[1,2,3]

public class demo01 {
    public static void main(String[] args) {
        int[] arr = {11, 2, 33, 4, 55};
        StringBuilder sb = xxx(arr);
        System.out.println(sb);

        String s = xxxx(arr);
        System.out.println(s);
    }

    //拼接成字符串:
    //1. + 拼接
    //2. StringBuilder.append拼接, 再转成String-->sb.toString()

    public static StringBuilder xxx(int[] arr) {

        StringBuilder sb = new StringBuilder("[");
        //遍历数组 得到数据
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]).append("]");
            } else {
                sb.append(arr[i]).append(",");
            }
        }
        return sb;
    }
    //------------------------------------------------
    public static String xxxx(int[] arr) {

        StringBuilder sb = new StringBuilder("[");
        //遍历数组 得到数据
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]).append("]");
            } else {
                sb.append(arr[i]).append(",");
            }
        }
        return sb.toString();     //返回值是String sb是StringBuilder,需要转成String
    }
}
